import os

import matplotlib.pyplot as plt
import numpy as np
import xlrd


def read_expends_xls(xls_path):
    data = xlrd.open_workbook(xls_path)
    table = data.sheet_by_index(0)  # 通过索引顺序获取
    return table.cell_value(22, 3)


def read_assets_xls(xls_path):
    data = xlrd.open_workbook(xls_path)
    table = data.sheet_by_index(1)  # 通过索引顺序获取
    value = table.cell_value(20, 4)
    if value == '':
        value = table.cell_value(19, 4)
    if value == '':
        value = 0
    return value


if __name__ == "__main__":
    root = r"D:\Work\test\xls"
    plt.rcParams['font.sans-serif'] = ['SimHei']
    xls_list = os.listdir(root)
    time_list = []
    expand_list = []
    asset_list = []
    plt.xticks(fontsize=12, rotation=30)
    for xls_name in xls_list:
        date = "-".join(xls_name.split("-")[:2])
        time_list.append(date)
        xls_file = os.path.join(root, xls_name)
        expand_list.append(read_expends_xls(xls_file))
        asset_list.append(read_assets_xls(xls_file))
    plt.subplot(211)
    plt.bar(time_list, expand_list)
    plt.axhline(np.mean(expand_list), color='green', linestyle='--', lw=2, alpha=0.7, label='average')
    plt.legend()
    plt.title("月消费")
    plt.grid(axis='y')
    plt.subplot(212)
    plt.bar(time_list, asset_list)
    plt.grid(axis='y')
    plt.title("月资产")
    plt.show()
    pass


